In C++, the deque::operator[]()is used to access elements at a specified position in a deque container. It allows direct access to deque elements using an index value. This operator is commonly used for fast element access and modification in a deque. Unlike the at() function, it does not perform boundary checking.
When position pos is greater than the size of the container, the operator[]() function returns a value 0. On the other hand, the at() function throws an exception i.e out of range.
It has the following syntax:
pos: It defines the position of an element which is to be accessed.
It returns a reference to an element at position pos in the deque container.
Here, we are going to discuss several examples to demonstrate the deque operator[]() function.
This example demonstrates how to access deque elements using the operator[]() function.
Output:
mango is my favorite fruit
Explanation:
In this example, operator[]() function access each element of deque a.
This example demonstrates what happens when an invalid position is accessed using the operator[]() function.
Output:
0
Explanation:
In this example, operator[]() function tries to access the position which is greater than the size of the container. Therefore, it returns 0.
This example demonstrates how to modify deque elements using the operator[]() function.
Output:
10 100 30 200
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India